package com.wang.springbootinit.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.model.entity.TestGuide;
import com.wang.springbootinit.model.param.TestGuideQuery;
import com.wang.springbootinit.service.TestGuideService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 测试指南控制器
 */
@RestController
@RequestMapping("/admin/testguide")
@Api(tags = "测试指南接口")
public class TestGuideController {

    @Autowired
    private TestGuideService testGuideService;
    
    /**
     * 管理员发布测试指南
     */
    @PostMapping("/add")
    @ApiOperation("发布测试指南")
    public BaseResponse publish(@RequestParam("title") String title,
                                @RequestParam("content") String content,
                                @RequestParam(value = "video", required = false) MultipartFile video,
                                HttpServletRequest request) {
        testGuideService.publishGuide(title,content,video,request);
        return ResultUtils.success();
    }
    
    /**
     * 管理员修改测试指南
     */
    @PostMapping("/update")
    @ApiOperation("修改测试指南")
    public BaseResponse update(@RequestParam("id") Integer id,
                               @RequestParam("title") String title,
                               @RequestParam("content") String content,
                               @RequestParam(value = "video", required = false) MultipartFile video,
                               HttpServletRequest request) {
        testGuideService.updateGuide(id,title,content,video,request);
        return ResultUtils.success();
    }
    
    /**
     * 管理员下架测试指南
     */
    @PutMapping("/offline/{id}")
    @ApiOperation("下架测试指南")
    public BaseResponse offline(@PathVariable Long id) {
        testGuideService.offlineGuide(id);
        return ResultUtils.success();
    }
    
    /**
     * 管理员删除测试指南
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("删除测试指南")
    public BaseResponse delete(@PathVariable("id") Long id) {
        testGuideService.deleteGuide(id);
        return ResultUtils.success();
    }
    
    /**
     * 分页查询测试指南列表
     */
    @GetMapping("/page")
    @ApiOperation("分页查询测试指南")
    public BaseResponse page(TestGuideQuery query) {
        Page<TestGuide> page = testGuideService.pageGuides(query);
        return ResultUtils.success(page);
    }
    
    /**
     * 获取测试指南详情
     */
    @GetMapping("/get/{id}")
    @ApiOperation("获取测试指南详情")
    public BaseResponse detail(@PathVariable Long id) {
        TestGuide guide = testGuideService.getGuideDetail(id);
        return ResultUtils.success(guide);
    }
    
    /**
     * 上传指南视频
     */
    @PostMapping("/upload/video")
    @ApiOperation("上传指南视频")
    public BaseResponse uploadVideo(@RequestParam MultipartFile file) {
        String url = testGuideService.uploadVideo(file);
        return ResultUtils.success(url);
    }
}